﻿namespace Extensions.Web.UI
{
    using System;
    using System.Collections.Generic;
    using System.Data.Common;
    using System.Web.UI.WebControls;

    public class TransactionExtensions
    {
        private List<DbCommand> _dbCommandList = null;

        public void AddKeep(SqlDataSource source)
        {
            if ((source.InsertCommand != null) && (source.InsertCommand != ""))
            {
                source.Inserting += new SqlDataSourceCommandEventHandler(this.source_KeepBegin);
            }
            if ((source.UpdateCommand != null) && (source.UpdateCommand != ""))
            {
                source.Updating += new SqlDataSourceCommandEventHandler(this.source_KeepBegin);
            }
            if ((source.DeleteCommand != null) && (source.DeleteCommand != ""))
            {
                source.Deleting += new SqlDataSourceCommandEventHandler(this.source_KeepBegin);
            }
        }

        public void Complete()
        {
            bool flag = false;
            Exception exception = null;
            List<DbTransaction> list = new List<DbTransaction>();
            foreach (DbCommand command in this.DbCommandList)
            {
                DbConnection connection = command.Connection;
                connection.Open();
                DbTransaction item = connection.BeginTransaction();
                command.Transaction = item;
                list.Add(item);
                try
                {
                    this.ReplaceNullValues(command);
                    command.ExecuteNonQuery();
                }
                catch (Exception exception2)
                {
                    flag = true;
                    exception = exception2;
                    break;
                }
            }
            foreach (DbTransaction transaction2 in list)
            {
                if (flag)
                {
                    transaction2.Rollback();
                }
                else
                {
                    transaction2.Commit();
                }
            }
            if (flag && (exception != null))
            {
                throw exception;
            }
        }

        private void ReplaceNullValues(DbCommand command)
        {
            int count = command.Parameters.Count;
            for (int i = 0; i < count; i++)
            {
                if (command.Parameters[i].Value == null)
                {
                    command.Parameters[i].Value = DBNull.Value;
                }
            }
        }

        protected void source_KeepBegin(object sender, SqlDataSourceCommandEventArgs e)
        {
            this.DbCommandList.Add(e.Command);
            e.Cancel = true;
        }

        private List<DbCommand> DbCommandList
        {
            get
            {
                if (this._dbCommandList == null)
                {
                    this._dbCommandList = new List<DbCommand>();
                }
                return this._dbCommandList;
            }
        }
    }
}

